<script setup>

import {getCurrentInstance, ref} from "vue";
import {operateLogStatusList, operateTypeList} from "@/enum/enum";
import {enumToArray} from "@/utils/utils";
import {clearSysOperLog, deleteSysOperLog, listSysOperLog} from "@/api/system/log/oper_log";
import OperDetailForm from "@/views/log/OperDetailForm.vue";

const {proxy} = getCurrentInstance();
const sysOperLogList = ref([])
const total = ref(0)
const queryFormRef = ref();
const queryForm = ref({
  pageNo: 1,
  pageSize: 10,
  operLocation: null,
  title: null,
  operName: null,
  operType: null,
  status: null,
  timeArr: null,
})
const ids = ref([])
const operLogStatusOptions = enumToArray(operateLogStatusList);
const operTypeOptions = enumToArray(operateTypeList);
const operDetailFormRef = ref();

const getList = async () => {
  const {data} = await listSysOperLog(queryForm.value);
  total.value = data.total;
  sysOperLogList.value = data.list;
}

const handleQuery = () => {
  queryForm.value.pageNo = 1;
  getList();
}

const handleReset = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

const handleSelectionChange = (selection) => {
  ids.value = selection
}

const handleDelete = () => {
  const logId = ids.value;
  proxy.$modal.confirm(`确定删除选中的操作日志吗？`).then(async () => {
    await deleteSysOperLog(logId);
    proxy.$modal.okNotify('删除成功');
    await getList();
  })
}

const handleClear = () => {
  proxy.$modal.confirm(`确定清空操作日志吗？`).then(async () => {
    await clearSysOperLog();
    proxy.$modal.okNotify('清空成功');
    await getList();
  })
}

const handleDetail = (operId) => {
  operDetailFormRef.value.open(operId)
}

getList()

</script>

<template>
  <div class="app-container">

    <a-form :model="queryForm" ref="queryFormRef" layout="inline" class="demo-form-inline">
      <a-form-item label="操作地址" field="operLocation">
        <a-input v-model="queryForm.operLocation" placeholder="请输入操作地址"/>
      </a-form-item>
      <a-form-item label="系统模块" field="title">
        <a-input v-model="queryForm.title" placeholder="请输入系统模块"/>
      </a-form-item>
      <a-form-item label="操作人员" field="operName">
        <a-input v-model="queryForm.operName" placeholder="请输入操作人员"/>
      </a-form-item>
      <a-form-item label="操作类型" field="operType">
        <a-select v-model="queryForm.operType" placeholder="请选择操作类型">
          <a-option v-for="(item, index) in operTypeOptions" :key="index" :label="item.label" :value="item.value"/>
        </a-select>
      </a-form-item>
      <a-form-item label="操作状态" field="status">
        <a-select v-model="queryForm.status" placeholder="请选择操作状态">
          <a-option v-for="(item, index) in operLogStatusOptions" :key="index" :label="item.label" :value="item.value"/>
        </a-select>
      </a-form-item>
      <a-form-item label="登录时间" field="timeArr">
        <a-range-picker
            v-model="queryForm.timeArr"
            show-time
            :time-picker-props="{
    defaultValue:['00:00:00','23:59:59']
    }"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="YYYY-MM-DD HH:mm:ss"
        />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" @click="handleQuery">
          <icon icon="ep-search" size="14px" color="unset" class="mr-1"/>
          查询
        </a-button>
        <a-button plain @click="handleReset" class="ml-12px">
          <icon icon="ep-refresh" size="14px" color="unset" class="mr-1"/>
          重置
        </a-button>
      </a-form-item>
    </a-form>

    <a-space class="flex mb-5">
      <a-button status="danger" plain :disabled="!ids.length" class="flex items-center"
                @click="handleDelete" v-hasPermission="['system:oper_log:delete']">
        <icon icon="ep-delete" size="14px" color="unset" class="mr-1"/>
        删除
      </a-button>
      <a-button status="danger" plain class="flex items-center" @click="handleClear"
                v-hasPermission="['system:oper_log:clear']">
        <icon icon="ep-delete" size="14px" color="unset" class="mr-1"/>
        清空
      </a-button>
    </a-space>

    <a-table
        :data="sysOperLogList"
        :bordered="true"
        row-key="operId"
        size="large"
        @selection-change="handleSelectionChange"
        :row-selection="{
          type:'checkbox',
          showCheckedAll:true,
          onlyCurrent:false
        }"
    >
      <template #columns>
        <a-table-column type="checkbox" :width="55"/>
        <a-table-column data-index="operId" title="日志编号"/>
        <a-table-column data-index="title" title="模块名"/>
        <a-table-column data-index="operType" title="操作类型">
          <template #cell="{record}">
            <a-tag :color="operateTypeList[record.operType].type">{{ operateTypeList[record.operType].label }}
            </a-tag>
          </template>
        </a-table-column>
        <a-table-column data-index="operName" title="操作人员"/>
        <a-table-column data-index="operIp" title="操作IP地址"/>
        <a-table-column data-index="operLocation" title="操作地址"/>
        <a-table-column data-index="status" title="操作状态">
          <template #cell="{record}">
            <a-tag :color="operateLogStatusList[record.status].type">{{ operateLogStatusList[record.status].label }}
            </a-tag>
          </template>
        </a-table-column>
        <a-table-column data-index="costTime" title="操作耗时"/>
        <a-table-column data-index="operTime" title="操作日期" :width="220"/>
        <a-table-column fixed="right" title="操作" :width="150">
          <template #cell="{record}">
            <a-button class="ml-2" type="text" @click="handleDetail(record.operId)"
                      v-hasPermission="['system:oper_log:detail']">详情
            </a-button>
          </template>
        </a-table-column>
      </template>
    </a-table>

    <oper-detail-form ref="operDetailFormRef"/>
  </div>
</template>

<style scoped lang="scss">

</style>
